using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Speedball2 {
  public class Attributes {
    public int _offset;

    public int _health;
    public int _initialDir;
    public int _pos;

    public int _agr;
    public int _att;
    public int _def;
    public int _spd;
    public int _thr;
    public int _pow;
    public int _sta;
    public int _int;

    public Attributes (byte [] bytes, int offset) {
      _offset = offset;

      _health = bytes.ReadByte (offset++);
      _initialDir = bytes.ReadByte (offset++);
      _pos = bytes.ReadByte (offset++);
      _agr = bytes.ReadByte (offset++);
      _att = bytes.ReadByte (offset++);
      _def = bytes.ReadByte (offset++);
      _spd = bytes.ReadByte (offset++);
      _thr = bytes.ReadByte (offset++);
      _pow = bytes.ReadByte (offset++);
      _sta = bytes.ReadByte (offset++);
      _int = bytes.ReadByte (offset++);

      if ((offset - _offset) != Size)
        throw new Exception ();
    }

    public void DoMultipleOf10 () {
      DoMultipleOf10_Helper (ref _agr);
      DoMultipleOf10_Helper (ref _att);
      DoMultipleOf10_Helper (ref _def);
      DoMultipleOf10_Helper (ref _spd);
      DoMultipleOf10_Helper (ref _thr);
      DoMultipleOf10_Helper (ref _pow);
      DoMultipleOf10_Helper (ref _sta);
      DoMultipleOf10_Helper (ref _int);
    }

    private void DoMultipleOf10_Helper (ref int attribute) {
      attribute = (attribute / 10) * 10;
    }

    public static int Size {
      get {
        return (0xB);
      }
    }

    public static int GetAttributesBaseAddress (int personAttributesAddress, out AttributesTypeEnum personAttributesType) {
      if ((personAttributesAddress >= 0x40A6) && (personAttributesAddress < 0x412A)) {
        personAttributesType = AttributesTypeEnum.Team1;
        return (0x40A6);
      }
      if ((personAttributesAddress >= 0x412A) && (personAttributesAddress < 0x41AE)) {
        personAttributesType = AttributesTypeEnum.Team2;
        return (0x412A);
      }
      if ((personAttributesAddress >= 0x41AE) && (personAttributesAddress < 0x41B9)) {
        personAttributesType = AttributesTypeEnum.Temp;
        return (0x41AE);
      }
      if ((personAttributesAddress >= 0x43D8) && (personAttributesAddress < 0x449E)) {
        personAttributesType = AttributesTypeEnum.Market;
        return (0x43D8);
      }
      throw new Exception ();
    }

    public void Dump () {
      Console.Write ("{0:X2} ", _health);
      Console.Write ("{0:X2} ", _initialDir);
      Console.Write ("{0:X2} ", _pos);
      Console.WriteLine ();
      Console.Write ("{0:X2} ", _agr);
      Console.Write ("{0:X2} ", _att);
      Console.Write ("{0:X2} ", _def);
      Console.Write ("{0:X2} ", _spd);
      Console.Write ("{0:X2} ", _thr);
      Console.Write ("{0:X2} ", _pow);
      Console.Write ("{0:X2} ", _sta);
      Console.Write ("{0:X2} ", _int);
      Console.WriteLine ();
    }
  }

  public enum AttributesTypeEnum {
    Team1,
    Temp,
    Team2,
    Market
  }
}
